package com.cccyy.date;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author by zhoushupeng
 * @date 2019/07/05
 **/
public class QuarterDeal {
    final static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    public static void main(String[] args) {
        Date now = new Date();

        String specifyDate = quarterStart();
        System.out.println("当前时间所属季度最后一个月第21天:" + specifyDate);

    }

    /*
     * @description: 获取当前时间所属季度开始月第一天
     * @param: startDate
     * @return: startDate
     * @author: zhoushupeng
     * @date: 19/7/5 上午11:11
     */
    public static String quarterStart() {
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        int remainder = (calendar.get(Calendar.MONTH) + 1) % 3;
        int month = remainder != 0 ? calendar.get(Calendar.MONTH) + (3 - remainder) : calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 21);
        calendar.setTime(calendar.getTime());
        return sdf.format(calendar.getTime());
    }
}
